package base;

import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

/**
 * @author BF00233
 *         和 {@link InfiniteViewPager} 配合使用
 */

public class InfinitePagerAdapter extends PagerAdapter {

    private final PagerAdapter mInnerAdapter;

    public InfinitePagerAdapter(PagerAdapter adapter) {
        mInnerAdapter = adapter;
    }

    protected int getExtraPageCount() {
        return 2;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        if (position == 0) {
            return mInnerAdapter.instantiateItem(container, getRealCount() - 1);
        }
        if (position == getCount() - 1) {
            return mInnerAdapter.instantiateItem(container, 0);
        }
        return mInnerAdapter.instantiateItem(container, position - 1);
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    public int getRealCount() {
        return mInnerAdapter.getCount();
    }

    @Override
    public int getCount() {
        return getRealCount() + getExtraPageCount();
    }
}
